<script setup lang="ts">
import CameraCapture from "@/views/PublicCom/cameraCapture.vue";
import Upload from "@/views/PublicCom/ImageFilesUpload.vue";
import {useDataPublicStore} from '@/stores/dataPublicStore.ts';
import {onMounted, ref} from "vue";
import router from "@/router/index.ts";
import {ElMessage} from "element-plus";
import {useMaterialInStore} from "@/stores/materialInStore.ts";
import {useMaterialStore} from "@/stores/materialStore.js";


import {useRoute} from "vue-router";
import {useSaveOneMaterialInFormAPI} from "@/apis/materialIn.js";
import {getProjectListAPI} from "@/apis/projectInfo.js";

const route=useRoute();

const dataPublicStore=useDataPublicStore();

const materialInNumber=ref(0);

const description=ref('');

// const materialInStore=useMaterialInStore();

const materialStore=useMaterialStore();


// 新添加的材料的类型
interface materialType {
  brand: string;
  createBy: string;
  lastUpdateBy: string;
  createTime: number;
  updateTime: number;
  description: string;
  encode: string;
  id: number;
  layout: string;
  location: string;
  materialName: string;
  materialPhotoList: string;
  number: number;
  pieces: string;
  typeName: string;
  model:string;

}

// 设置了类型----新添加
const materialObject=ref<materialType>({
  brand: '',
  createBy: '',
  lastUpdateBy: '',
  createTime: 0,
  updateTime: 0,
  description: '',
  encode: '',
  id: 0,
  layout: '',
  location: '',
  materialName: '',
  materialPhotoList: '',
  number: 0,
  pieces: '',
  typeName: '',
  model:''
});

const receiveFromProjectList=ref([]);
const receiveFromProject=ref({})

onMounted(async ()=>{
  // console.log(route.params.id);
  if(route.params.id===null||route.params.id===undefined){
    await router.push("/home/material");
  }
  else {
    await materialStore.getOneByIdStore(route.params.id);
    materialObject.value=materialStore.oneMaterial;
  }

  await getProjectListAPI(1,100000).then(res=>{
    receiveFromProjectList.value=res.data;
  })

})

// 显示进度条配合函数
const format=(percentage:number)=>{
  return percentage===100?'100%':`${percentage}`
}

const disableSwitch=ref(false);
/*todo 是否使用表单还是用其他方式，待定*/
const  submitSaveOneTransfer=async ()=>{
  if (materialInNumber.value<=0){
    ElMessage({
      type:"warning",
      message:'数量不能小于等于0！'
    })
    return;
  }

  const formData=new FormData();
  formData.append('materialName',materialObject.value.materialName);
  formData.append('brand',materialObject.value.brand);
  formData.append('description',description.value);
  formData.append('number',materialInNumber.value);
  formData.append('pieces',materialObject.value.pieces);
  formData.append('model',materialObject.value.model);
  formData.append('typeName',materialObject.value.typeName);
  formData.append('layout',materialObject.value.layout);
  formData.append('price',price.value);
  formData.append('totalPrice',totalPrice.value);
  formData.append('receiveProject',receiveFromProject.value.name);

  if (dataPublicStore.photoFileList.length>0){
    dataPublicStore.photoFileList.forEach((item:File)=>{
      formData.append('photoFiles',item);
    })
  }

  if (dataPublicStore.uploadFileList.length>0){
    dataPublicStore.uploadFileList.forEach((item:File)=>{
      formData.append('photoFiles',item);
    })
  }


  await useSaveOneMaterialInFormAPI(formData)
  disableSwitch.value=true;
  dataPublicStore.setPhotoFiles([]);
  dataPublicStore.setUploadFileList([]);
  dataPublicStore.setOnUploadProgress(0);
  dataPublicStore.saveDataObject({});
  await router.push({path: '/home/material/'})
}


const price=ref(0);
const totalPrice=ref(0);
const getTotalPrice=()=>{
  totalPrice.value=Math.round(price.value*materialInNumber.value*100)/100;
}



</script>

<template>
  <!--  进度条-->
  <el-progress :percentage="dataPublicStore.onUploadProgress" :format="format"    :stroke-width="5" v-show="dataPublicStore.onUploadProgress" style="margin: 0;padding: 0" ></el-progress>
  <!-- 面包屑 -->
  <div class="bread-container">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: `/` }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/home/material/`}" >材料列表</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:`/home/material/materialInList`}" >材料进场列表</el-breadcrumb-item>
      <el-breadcrumb-item  >材料进场详情</el-breadcrumb-item>
    </el-breadcrumb>
  </div>


  <div class="divTable">
    <table class="table">
      <tbody>
      <tr>
        <td class="tableTitle" colspan="4" >材    料    进     场    表    单</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF;color: #242424;font-family: 微软雅黑,serif">材料名称</td>
        <td style="width: 350px;height: 15px;   text-align: center  ">{{materialObject.materialName}}</td>
        <td style="width: 250px;height: 15px; text-align: center ;background: #EFF6FF"  >材料品牌</td>
        <td style="width: 350px;height: 15px;   text-align: center  ">{{materialObject.brand}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料类型</td>
        <td style="width: 350px;height: 15px;text-align: center;">{{materialObject.typeName}}</td>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料型号</td>
        <td style="width: 350px;height: 15px;text-align: center;">{{materialObject.model}}</td>
      </tr>
      <tr>
        <td style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">材料数量单位</td>
        <td style="width: 350px;height: 15px;text-align: center;">{{materialObject.pieces}}</td>
        <!--        todo 这里要修改，记住-->
        <td style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >原库存数量</td>
        <td style="width: 350px;height: 15px;text-align: center   ">
          {{materialObject.number}}
        </td>
      </tr>
      <tr>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >材料所在仓库</td>
        <td  style="width: 350px;height: 15px;text-align: center   ">
          {{materialObject.location}}
        </td>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >材料进场数量</td>
        <td  style="width: 350px;height: 15px;text-align: center   ">
          <el-input  v-model="materialInNumber" />
        </td>
      </tr>

      <tr>
        <td   style="width: 250px;height: 15px;text-align: center ;background: #EFF6FF"  >

          <el-tooltip placement="top">
            <template #content> <h3>
              如果是新购买，单价按实际填报，
              从项目中调回仓库，单价应为0
            </h3>
            </template>
            材料单价
          </el-tooltip>
        </td>
        <td  style="width: 350px;height: 15px;text-align: center   ">
          <el-input @change="getTotalPrice" v-model="price" />
        </td>
        <td   style="width: 150px;height: 15px;text-align: center ;background: #EFF6FF"  >材料总价</td>
        <td  style="width: 150px;height: 15px;text-align: center   ">
          <el-input disabled  v-model="totalPrice" />
        </td>
      </tr>

      <tr>
        <td  style="text-align: center;background: #EFF6FF">材料来源地</td>
        <td  style="text-align: center">
          <el-select
              value-key="id"
              v-model="receiveFromProject"
              placeholder="Select"
              size="large"
          >
            <el-option
                v-for="item in receiveFromProjectList"
                :key="item.id"
                :label="item.name"
                :value="item"
            />
          </el-select>
        </td>

        <td  style="ext-align: center;background: #EFF6FF">备注</td>
        <td style="text-align: center">
          <el-input  v-model="description" />
        </td>
      </tr>
      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF"></td>
      </tr>
      <tr>
        <td colspan="4">
          <camera-capture/>
        </td>
      </tr>
      <tr>
        <td colspan="4">
          <upload/>
        </td>
      </tr>
      <tr>
        <td colspan="4" style="width: 250px;height: 15px;text-align: center;background: #EFF6FF">
          <el-button
              type="success"
              size="large"
              @click="submitSaveOneTransfer"
          >
            确定
          </el-button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>

</template>

<style scoped lang="scss">
.bread-container {
  padding: 25px 10px;
  margin-left: 10px;
}
.divTable {
  position: absolute;
  top: 150px;
  bottom: 0;
  left: 300px;
  right: 0;
  margin: auto;
}


table, td,tr{
  border: 1px solid #44a8da;
  text-align: center;
  border-spacing: 0;
  border-collapse: collapse;
  height: 45px;
}

.tableTitle{

  border-right: 0 none;
  border-left: 0 none;
  text-align: center;
  font-size: large;
  font-weight: bold;
}

.demo-image__error .image-slot {
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}
.demo-image__error .el-image {
  width: 100%;
  height: 200px;
}

</style>